#region

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using Volo.Abp;
using Volo.Abp.BlobStoring;
using Volo.Abp.BlobStoring.FileSystem;
using Volo.Abp.BlobStoring.Minio;
using Volo.Abp.Modularity;

#endregion

namespace Leo.Core.Shared.Module
{
    /// <summary>
    ///     后台任务模块
    /// </summary>
    [DependsOn(
        typeof(AbpBlobStoringModule),
        typeof(AbpBlobStoringFileSystemModule),
        typeof(AbpBlobStoringMinioModule)
    )]
    public class BlobModule : AbpModule
    {
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var configuration = context.GetConfiguration();
        }

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            ConfigureBlob(configuration);
        }

        private void ConfigureBlob(IConfiguration configuration)
        {
            var blobMode = configuration["Blob:Mode"];

            if (!string.IsNullOrEmpty(blobMode) && blobMode.Equals("Minio", StringComparison.OrdinalIgnoreCase))
            {
                var endPoint = configuration["Blob:Minio:EndPoint"];
                var accessKey = configuration["Blob:Minio:AccessKey"];
                var secretKey = configuration["Blob:Minio:SecretKey"];
                var bucketName = configuration["Blob:Minio:BucketName"];
                Configure<AbpBlobStoringOptions>(options =>
                {
                    options.Containers.ConfigureDefault(container =>
                    {
                        container.UseMinio(minio =>
                        {
                            minio.EndPoint = endPoint;
                            minio.AccessKey = accessKey;
                            minio.SecretKey = secretKey;
                            minio.BucketName = bucketName;
                        });
                    });
                });
            }
            else
            {
                var path = configuration["Blob:Path"];
                path = string.IsNullOrEmpty(path) ? "Files" : path;
                var basePath = Path.Combine(Directory.GetCurrentDirectory(), path);
                Configure<AbpBlobStoringOptions>(options =>
                {
                    options.Containers.ConfigureDefault(container =>
                    {
                        container.UseFileSystem(fileSystem =>
                        {
                            fileSystem.BasePath = basePath;
                        });
                    });
                });
            }
        }
    }
}